package com.zy.mc_module.utils.retrofit.error;

import android.net.ParseException;
import android.util.Log;

import com.google.gson.JsonParseException;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.ConnectException;

import okhttp3.ResponseBody;

/**
 * Created on 18/10/23
 * 异常处理类
 */
public class BaseException {
    private static JSONObject jsonObject_error;

    public static ThdException handleException(Throwable e) {
        String msg;
        ThdException thdException;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ResponseBody responseBody = httpException.response().errorBody();
            String url = httpException.response().raw().request().url().toString();
            Log.e("OkHttp", url);
            try {
                //获取具体的错误信息,如果code=480就单独处理
                msg = responseBody.string();
            } catch (Exception exc) {
                msg = e.getMessage();
                exc.printStackTrace();
            }
            thdException = new ThdException(e, httpException.code());
            switch (httpException.code()) {
//                case 400:
//                    thdException.message = "请求无效，请稍后再试";
//                    break;
//                case 401:
//                    thdException.message = "请求错误，请稍后再试";
//                    break;
//                case 403:
//                    thdException.message = "服务器拒绝请求，请稍后再试";
//                    break;
                case 404:
                    thdException.message = "请求地址无效，请稍后再试  404";
                    break;
//                case 408:
//                    thdException.message = "请求超时，请稍后再试";
//                    break;
//                case 504:
//                    thdException.message = "网络连接异常，请检查网络";
//                    break;
//                case 500:
//                    thdException.message = "服务器内部错误，请稍后再试";
//                    break;
//                case 503:
//                    thdException.message = "服务不可用，请稍后再试";
//                    break;
//                case 479:
//                    thdException.message = "登录过期，请重新登录";
//                    break;
                default:
                    thdException.message = msg;
                    break;
            }
            return thdException;
        } else if (e instanceof ServerException) {
            ServerException resultException = (ServerException) e;
            thdException = new ThdException(resultException, resultException.code);
            thdException.message = resultException.message;
            return thdException;
        } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
            thdException = new ThdException(e, ERROR.PARSE_ERROR);
            thdException.message = "解析错误" + e.getLocalizedMessage();
            return thdException;
        } else if (e instanceof ConnectException) {
            thdException = new ThdException(e, ERROR.NETWORD_ERROR);
            thdException.message = "连接失败";
            return thdException;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            thdException = new ThdException(e, ERROR.SSL_ERROR);
            thdException.message = "证书验证失败";
            return thdException;
        } else if (e instanceof java.net.SocketTimeoutException) {
            thdException = new ThdException(e, ERROR.TIMEOUT_ERROR);
            thdException.message = "请求超时，请稍后再试";
            return thdException;
        } else if (e instanceof java.net.UnknownHostException) {
            thdException = new ThdException(e, ERROR.TIMEOUT_ERROR);
            thdException.message = "网络中断，请检查网络状态";
            return thdException;
        } else if (e instanceof NullPointerException || e instanceof java.io.EOFException) {
            thdException = new ThdException(e, ERROR.PARSE_EmptyERROR);
            thdException.message = "获取数据失败，请稍后再试";
            return thdException;
        } else {
            //未知错误
            thdException = new ThdException(e, ERROR.UNKNOWN);
            thdException.message = e.getMessage();
            return thdException;
        }
    }

    /**
     * 约定异常
     */
    public class ERROR {
        /**
         * 未知错误
         */
        public static final int UNKNOWN = 1000;
        /**
         * 解析错误
         */
        public static final int PARSE_ERROR = 1001;
        /**
         * 解析no content错误
         */
        public static final int PARSE_EmptyERROR = 1007;
        /**
         * 网络错误
         */
        public static final int NETWORD_ERROR = 1002;
        /**
         * 证书出错
         */
        public static final int SSL_ERROR = 1005;
        /**
         * 连接超时
         */
        public static final int TIMEOUT_ERROR = 1006;
    }

    public class ServerException extends RuntimeException {
        public int code;
        public String message;

        public ServerException(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }
}
